source('code/analysis/load.R')

pew = read_dta('data/working/pew.dta') %>%
  mutate(exp = 'pew') %>%
  dplyr::select(black, asian, age, hisp, male, white, political_party, political_lean, political_party_lean, hs, bachelors, weight, exp) 


## (Exp.1, Exp.1R, Exp.3, Aux Exp.2, Aux Exp.3, Aux Exp.6) ##

sender = read_dta('data/working/deport-sender.dta') %>%
  mutate(exp = 'deport') %>% 
  bind_rows(read_dta('data/working/defund-sender.dta') %>% mutate(exp = 'defund', replication=0)) %>% 
  bind_rows(read_dta('data/working/defund-sender-replication.dta') %>% mutate(exp = 'defund-replication', replication=1)) %>% 
  bind_rows(read_dta('data/working/rainforest-sender.dta') %>% mutate(exp = 'rainforest')) %>% 
  bind_rows(read_dta('data/working/defund-hypothetical.dta') %>% mutate(exp = 'defund-hypo')) %>%
  bind_rows(read_dta('data/working/daylight-sender.dta') %>% mutate(exp = 'dst')) %>%
  data_finalize() %>%
  mutate(exp1 = ((exp == 'defund' | exp == 'defund-replication')),
         placebo = (exp=='rainforest'|exp=='dst'),
         exp3 = exp=='deport',
         expgroup = case_when(
           exp1 ~ 'Experiments 1 and 1R',
           exp3 ~ 'Experiment 3',
           placebo ~ 'Auxiliary Experiments 2 and 3\n(placebos)'
         ),
         expgroup = factor(expgroup, levels=c('Experiments 1 and 1R','Experiment 3','Auxiliary Experiments 2 and 3\n(placebos)'))) %>%
  filter(join==1) 

# Balance table 
for (type_exp in c('deport','defund', 'defund-replication', 'rainforest','dst')) {
  make_balance_sender(sender %>% filter(exp==type_exp))
}

# Merge Sub-tables in Exp.1
out1=readLines('output/tables/defund-sender-balance.tex')
out2=readLines('output/tables/defund-sender-replication-balance.tex')

out = c(
  out1[1:3],
  '\\label{t:defund-sender-balance}',
  out1[5:12],
  '\\textbf{Panel A:} & \\multicolumn{5}{c}{Experiment 1} \\\\ \\midrule',
  out1[13:26],
  '\\midrule',
  '\\textbf{Panel B:} & \\multicolumn{5}{c}{Replication of Experiment 1} \\\\ \\midrule',
  out2[13:length(out2)]
)
write_lines(out, str_interp('output/tables/defund-sender-pool-balance.tex'))

file.remove('output/tables/defund-sender-balance.tex')
file.remove('output/tables/defund-sender-replication-balance.tex')

# Merge Sub-tables in Aux Exp.6
for (cred in c('credible', 'notcredible')) {
  make_balance_sender(sender %>% filter(exp=='defund-hypo', credibility==cred, !is.na(posted), socialmedia %in% c('Facebook','Twitter'), vote!='Donald Trump'))
}
out1=readLines('output/tables/defund-hypo-hc-balance.tex')
out2=readLines('output/tables/defund-hypo-lc-balance.tex')

out = c(
  out1[1:3],
  '\\label{t:defund-hypo-balance}',
  out1[5:12],
  '\\textbf{Panel A: } & \\multicolumn{5}{c}{Higher-credibility} \\\\ \\midrule',
  out1[13:26],
  '\\midrule',
  '\\textbf{Panel B: } & \\multicolumn{5}{c}{Lower-credibility} \\\\ \\midrule',
  out2[13:length(out2)]
)
write_lines(out, str_interp('output/tables/defund-hypo-balance.tex'))

file.remove('output/tables/defund-hypo-hc-balance.tex')
file.remove('output/tables/defund-hypo-lc-balance.tex')


# Representativeness table 
for (type_exp in c('deport','defund', 'defund-replication', 'rainforest', 'dst')) {
  make_represent_sender(sender, type_exp)
}

# Merge Sub-tables in Exp.1
out1=readLines('output/tables/defund-sender-representativeness.tex')
out2=readLines('output/tables/defund-sender-replication-representativeness.tex')

out = c(
  out1[1:3],
  '\\label{t:defund-sender-representativeness}',
  out1[5:10],
  '\\textbf{Panel A:} & \\multicolumn{2}{c}{Experiment 1} \\\\ \\midrule',
  out1[12:22],
  '\\midrule',
  out2[9:10],
  '\\textbf{Panel B:} & \\multicolumn{2}{c}{Replication of Experiment 1} \\\\ \\midrule',
  out2[12:length(out2)]
)
write_lines(out, str_interp('output/tables/defund-sender-pool-representativeness.tex'))

file.remove('output/tables/defund-sender-representativeness.tex')
file.remove('output/tables/defund-sender-replication-representativeness.tex')

# Merge Sub-tables in Aux Exp.6
for (cred in c('credible', 'notcredible')) {
  make_represent_sender(data=sender %>% filter((credibility==cred & !is.na(posted) & socialmedia %in% c('Facebook','Twitter') & vote!='Donald Trump') | exp == 'pew'), type='defund-hypo')
}

out1=readLines('output/tables/defund-hypo-hc-representativeness.tex')
out2=readLines('output/tables/defund-hypo-lc-representativeness.tex')

out = c(
  out1[1:3],
  '\\label{t:defund-hypo-representativeness.tex}',
  out1[5:10],
  '\\textbf{Panel A: } & \\multicolumn{2}{c}{Higher-credibility} \\\\ \\midrule',
  out1[12:22],
  '\\midrule',
  '\\textbf{Panel B: }& \\multicolumn{2}{c}{Lower-credibility} \\\\ \\midrule',
  out2[12:length(out2)]
)
write_lines(out, str_interp('output/tables/defund-hypo-representativeness.tex'))

file.remove('output/tables/defund-hypo-hc-representativeness.tex')
file.remove('output/tables/defund-hypo-lc-representativeness.tex')






## (Exp.2, Exp.4, Aux Exp.7) ##

receiver = read_dta('data/working/defund-receiver.dta') %>%  
  mutate(exp = 'defund', credibility = 'credible', expgroup = 'Experiment 2\n(defund, higher credibility)') %>% 
  bind_rows(read_dta('data/working/deport-receiver.dta') %>% mutate(exp = 'deport', expgroup='Experiment 4\n(deport)')) %>% 
  bind_rows(read_dta('data/working/defund-nocredible-receiver.dta') %>% mutate(exp = 'notcredible', credibility = 'notcredible', expgroup='Auxiliary Experiment 7\n(defund, lower credibility)')) %>% 
  data_finalize() %>%
  mutate(deny = !bonus,
         expgroup = factor(expgroup, levels= c('Experiment 2\n(defund, higher credibility)','Auxiliary Experiment 7\n(defund, lower credibility)','Experiment 4\n(deport)')))

# Balanced table
for (type_exp in c('deport', 'defund', 'notcredible')) {
  make_balance_receiver(receiver %>% filter(exp==type_exp))
}

# Representativeness table
for (type_exp in c('deport','defund','notcredible')) {
  make_represent_receiver(receiver, type_exp)
}




## (Aux Exp.1, Aux Exp.4, Aux Exp.5, Aux Exp.8) ##

sanctions = haven::read_dta('data/raw/sanctions.dta') %>% 
  filter(attention==1) %>% 
  filter(row_number()<515) %>%
  mutate(age = year,
         age2 = age^2,
         hisp = as.numeric(hispanic==1),
         male = as.numeric(sex==1),
         white = as.numeric(race==3),
         black = as.numeric(race==1),
         asian = as.numeric(race==2),
         hs = as.numeric(education > 1),
         bachelors=as.numeric(education > 4),
         exp='sanction') %>%
  dplyr::select(age, age2, hisp, male, white, black, asian, hs, bachelors, exp)

pursuasion = read_dta('data/working/defund-persuasion.dta') %>%
  mutate(exp = 'purs-defund') %>% 
  bind_rows(read_dta('data/working/deport-persuasion.dta') %>% mutate(exp = 'purs-deport', sample = 'lucid') %>% rename(policy = deport)) %>% 
  bind_rows(read_dta('data/working/tucker-persuasion-prolific.dta') %>% mutate(exp = 'purs-deport', sample = 'prolific') %>% rename(policy = deport)) %>% 
  data_finalize_persuasion() %>%
  dplyr::select(info, age, age2, hisp, male, white, black, asian, hs, bachelors, exp, ind, partisan_num)
  
motive = read_dta('data/working/defund-openended.dta') %>% 
  mutate(exp = 'motive') %>%
  data_finalize_motive() %>%
  dplyr::select(excuse, noexcuse, age, age2, hisp, male, white, black, asian, hs, bachelors, exp, ind, partisan_num)

other = read_dta('data/working/anticipated-persuasion.dta') %>% # anticipated persuasion
  data_finalize() %>%
  mutate(exp = 'anticipated',
         ) %>%
  dplyr::select(excuse, noexcuse, age, age2, hisp, male, white, black, asian, hs, bachelors, exp, ind, partisan_num) %>%
  bind_rows(sanctions) %>%
  bind_rows(pursuasion) %>%
  bind_rows(motive)

# Balanced tables
for (type_exp in c('anticipated', 'motive')) {
  make_balance_other(other %>% filter(exp==type_exp))
}
for (type_exp in c('purs-deport','purs-defund')) {
  make_balance_persuasion(other %>% filter(exp==type_exp))
}

# Representativeness table
for (type_exp in c('sanction', 'anticipated','motive','purs-deport','purs-defund')) {
  make_represent_other(other, type_exp)
}





